package edu.neusoft.android.yuejian14.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.activity.PicDetailActivity;
import edu.neusoft.android.yuejian14.javabean.Pic;

import static edu.neusoft.android.yuejian14.Const_Pic.BASE_URL;


public class PicListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<Pic> mDataList;
    private LayoutInflater mLayoutInflater;

    public PicListAdapter(Context mContext, List<Pic> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public PicListAdapter(Context context) {

        this(context, new ArrayList<Pic>());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.inflate(R.layout.item_pic ,parent, false);
        return new ViewHolder(mContext, v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (mDataList == null || mDataList.isEmpty()) {
            return;
        }

        final Pic entity = mDataList.get(position);

        if (null == entity) {
            return;
        }

        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setView(entity);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(mContext, PicDetailActivity.class);
                intent.putExtra("cardid",entity.getCardid());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    public Pic getItem(int position) {
        return mDataList == null ? null : mDataList.get(position);
    }

    public void setData(List<Pic> list) {
        this.mDataList = list;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
       private  static ImageView pic;
        private static ImageView my_photo;
        Context mContext;
        TextView name;
        TextView time;
        TextView topic;

        public ViewHolder(Context context, View itemView) {
            super(itemView);
            mContext = context;
            name = (TextView) itemView.findViewById(R.id.name);
            time= (TextView) itemView.findViewById(R.id.time);
            topic= (TextView) itemView.findViewById(R.id.topic);
            pic= (ImageView) itemView.findViewById(R.id.pic);
            my_photo=(ImageView) itemView.findViewById(R.id.my_photo);

        }

        void setView(Pic entity) {

            name.setText(entity.getUsername());
            time.setText(entity.getCardtime());
            topic.setText(entity.getTopic());
            String p_pic= BASE_URL + entity.getCardpic();
            if (! p_pic.equals("")) {
                Picasso.with(mContext).load(p_pic).error(R.drawable.no_pic).into(ViewHolder.pic);
            }

            String my_photo= BASE_URL + entity.getPhoto();
            if (!my_photo.equals("")) {
                Picasso.with(mContext).load(my_photo).error(R.drawable.no_pic).into(ViewHolder.my_photo);
            }
        }

    }
}
